package com.anxin.act.task.web;

import com.anxin.act.task.entity.ViewHiTask;
import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.task.service.ActTaskService;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.common.exception.BaseException;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.framework.aspectj.lang.annotation.Log;
import com.anxin.framework.aspectj.lang.enums.BusinessType;
import com.anxin.framework.security.LoginUser;
import com.anxin.framework.security.service.TokenService;
import com.anxin.framework.web.controller.BaseController;
import com.anxin.framework.web.entity.AjaxResult;
import com.anxin.framework.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 流程任务处理
 *
 * @author: liuxiangyu
 * @date: 2020/3/12 18:09
 */
//@Api(tags = "待办处理")
@RestController
@RequestMapping("/act/task")
public class ActTaskController extends BaseController {

    @Autowired
    private ActTaskService actTaskService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询待办
     */
    //@ApiOperation(value = "查询待办列表")
    @GetMapping("todoList")
    @Log(title = "查询待办列表", businessType = BusinessType.LIST, importentName = "title")
    public TableDataInfo<TaskVo> todoList(ViewRuTask viewRuTask) {
        return getDataTable(actTaskService.todoList(viewRuTask));
    }


    /**
     * 查询已办
     */
    //@ApiOperation(value = "查询已办列表")
    @GetMapping("hiList")
    @Log(title = "查询已办列表", businessType = BusinessType.LIST, importentName = "title")
    public TableDataInfo<TaskVo> hiList(TaskVo taskVo) {
        return getDataTable(actTaskService.hiList(taskVo));
    }

    /**
     * 获取待办相关信息
     *
     * @param id
     * @return
     */
    //@ApiOperation(value = "获取待办相关信息")
    @GetMapping(value = "/getTask/{id}")
    @Log(title = "获取待办相关信息", businessType = BusinessType.LIST)
    public AjaxResult<TaskVo> getTask(@PathVariable("id") String id, HttpServletRequest httpRequest) {
        TaskVo task = actTaskService.getTask(id);
      /*  // 查看是否越权
        String token = tokenService.getToken(httpRequest);
        LoginUser loginUser = tokenService.getLoginUser(token);
        if(loginUser !=null && StringUtils.isNotBlank(task.getAssignee()) && !StringUtils.equals(task.getAssignee(),loginUser.getUserName())){
            throw new BaseException("数据越权，无法查看");
        }*/
        return AjaxResult.success(task);
    }

    /**
     * 查询待办数量
     *
     * @return
     */
    //@ApiOperation(value = "查询待办数量")
    @GetMapping(value = "/todoCount")
    @Log(title = "查询待办数量", businessType = BusinessType.LIST)
    public AjaxResult todoCount() {
        return AjaxResult.success(actTaskService.getCount(UserUtils.getUser().getId()));
    }

    /**
     * 查询来源节点用于驳回
     *
     * @param id 待办离开
     * @return 待办之前的节点集合
     */
    //@ApiOperation(value = "查询来源节点用于驳回")
    @GetMapping(value = "/getHistoryNode/{id}")
    @Log(title = "查询来源节点用于驳回", businessType = BusinessType.LIST)
    public AjaxResult getHistoryNode(@PathVariable("id") String id) {
        return AjaxResult.success(actTaskService.getHistoryNode(id));
    }

    /**
     * 签收流程
     *
     * @return
     */
    //@ApiOperation(value = "签收流程")
    @GetMapping(value = "/claimTask/{id}")
    @Log(title = "签收流程", businessType = BusinessType.UPDATE)
    public AjaxResult claimTask(@PathVariable("id") String id) {
        return actTaskService.claimTask(id);
    }

    /**
     * 撤回任务校验
     *
     * @param taskId
     * @return
     */
    //@ApiOperation(value = "撤回任务校验")
    @GetMapping(value = "/withdrawCheck/{taskId}")
    @Log(title = "撤回任务校验", businessType = BusinessType.LIST)
    public AjaxResult withdrawCheck(@PathVariable("taskId") String taskId) {
        return actTaskService.withdrawCheck(taskId);
    }


    /**
     * 撤回任务
     *
     * @param taskId
     * @return
     */
    //@ApiOperation(value = "撤回任务")
    @GetMapping(value = "/withdrawTask/{taskId}")
    @Log(title = "撤回任务", businessType = BusinessType.UPDATE)
    public AjaxResult withdrawTask(@PathVariable("taskId") String taskId) {
        actTaskService.withdrawTask(taskId);
        return AjaxResult.success();
    }

    /**
     * 校验任务是否可以驳回
     *
     * @param taskId 任务ID
     * @return
     */
    @GetMapping(value = "/rejectedCheck/{taskId}")
    public AjaxResult rejectedCheck(@PathVariable("taskId") String taskId) {
        return actTaskService.rejectedCheck(taskId);
    }


    /**
     * 提交代办
     *
     * @param taskVo
     */
    //@ApiOperation(value = "提交代办任务")
    @PostMapping(value = "/complete")
    @Log(title = "提交待办任务", businessType = BusinessType.INSERT, importentName = "title")
    public AjaxResult complete(@RequestBody TaskVo taskVo) {
        return actTaskService.complete(taskVo);
    }

    //@ApiOperation(value = "获取任务状态,是待办还是已办")
    @GetMapping(value = "/getTaskStatus/{taskId}")
    public AjaxResult getTaskStatus(@PathVariable("taskId") String taskId) {
        return actTaskService.getTaskStatus(taskId);
    }

    /**
     * 保存补充意见
     *
     * @param taskVo
     */
    //@ApiOperation(value = "保存补充意见")
    @PostMapping(value = "/saveSupplementaryComments")
    @Log(title = "补充意见", businessType = BusinessType.INSERT)
    public AjaxResult saveSupplementaryComments(@RequestBody TaskVo taskVo) {
       return actTaskService.saveSupplementaryComments(taskVo);
    }

    /**
     * 查询补充意见
     */
    //@ApiOperation(value = "查询补充意见")
    @GetMapping(value = "/findSupplementaryComments")
    @Log(title = "查询补充意见", businessType = BusinessType.LIST)
    public AjaxResult<TaskVo>  findSupplementaryComments(TaskVo taskVo) {
        return  AjaxResult.success(actTaskService.findSupplementaryComments(taskVo));
    }

    /**
     * 暂存
     */
    //@ApiOperation(value = "暂存")
    @PostMapping(value = "/actTaskOpinionStaging")
    @Log(title = "暂存", businessType = BusinessType.LIST)
    public AjaxResult<TaskVo>  staging(@RequestBody TaskVo taskVo) {
        return  AjaxResult.success(actTaskService.staging(taskVo));
    }

    /**
     * 历史任务管理
     * @param viewHiTask
     * @return
     */
    //@ApiOperation(value = "历史任务")
    @PostMapping(value = "/hiTaskManager")
    @Log(title = "历史任务", businessType = BusinessType.LIST)
    public TableDataInfo<ViewHiTask> hiTaskManager(@RequestBody ViewHiTask viewHiTask){
        return getDataTable(actTaskService.hiTaskManager(viewHiTask));
    }

}
